local hx__fankui = fk.CreateSkill {
  name = "hx__fankui",
}

hx__fankui:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__fankui.name) and player:usedSkillTimes(hx__fankui.name, Player.HistoryTurn) < 2
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|.",
    }
    room:judge(judge)
    local suit = judge.card:getSuitString()
    local recorded = tostring(player:getMark("hx__fankuiRecord") or "")
    local suits = recorded and recorded:split("+") or {}
    if not table.contains(suits, suit) then
      table.insert(suits, suit)
      room:addPlayerMark(player, "hx__fankui_suitrecord", 1)
      room:setPlayerMark(player, "hx__fankuiRecord", table.concat(suits, "+"))
      local card_suits_table = { spade="♠", club="♣", heart="♥", diamond="♦" }
      room:setPlayerMark(player, "@hx__fankui", table.concat(table.map(suits, function(s) return card_suits_table[s] end), ""))
    end
    local x = data.damage
    local target_role = (event == fk.Damaged) and data.from or data.to
    local choices = {"hx__fankui11", "hx__fankui22"}
    if player:getMark("hx__fankui_suitrecord") >= 4 then
      table.insert(choices, "beishui")
    end
    local choice = room:askToChoice(player,{
        choices = choices,
        skill_name = hx__fankui.name,
        prompt = "#hx__fankui-choose",
    })
    if choice == "beishui" then
      room:setPlayerMark(player, "hx__fankuiRecord", 0)
      room:setPlayerMark(player, "@hx__fankui", 0)
      room:setPlayerMark(player, "hx__fankui_suitrecord", 0)
    end
    if choice == "hx__fankui11" or choice == "beishui" then
      room:obtainCard(player, judge.card, false, fk.ReasonPrey)
      if target_role and target_role:isAlive() and not target_role:isNude() then
        local car = room:askToChooseCards(player, {
          target = target_role,
          min = x,
          max = x,
          flag = "he",
          skill_name = hx__fankui.name,
        })
        room:throwCard(car, self.name, target_role, player)
      end
    end
    if choice == "hx__fankui22" or choice == "beishui" then
      if target_role and target_role:isAlive() and not target_role:isNude() then
        local card = room:askToChooseCards(player, {
          target = target_role,
          min = 1,
          max = 1,
          flag = "he",
          skill_name = hx__fankui.name,
        })
        room:obtainCard(player, card, false, fk.ReasonPrey)
      end
      local to = room:askToChoosePlayers(player, {
        targets = room:getOtherPlayers(player, false),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__fankui-choose_target",
        skill_name = hx__fankui.name,
        cancelable = true,
      })
      if #to > 0 then
        to[1]:drawCards(x, self.name)
      end
    end
  end
})

hx__fankui:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__fankui.name) and player:usedSkillTimes(hx__fankui.name, Player.HistoryTurn) < 2
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|.",
    }
    room:judge(judge)
    local suit = judge.card:getSuitString()
    local recorded = tostring(player:getMark("hx__fankuiRecord") or "")
    local suits = recorded and recorded:split("+") or {}
    if not table.contains(suits, suit) then
      table.insert(suits, suit)
      room:addPlayerMark(player, "hx__fankui_suitrecord", 1)
      room:setPlayerMark(player, "hx__fankuiRecord", table.concat(suits, "+"))
      local card_suits_table = { spade="♠", club="♣", heart="♥", diamond="♦" }
      room:setPlayerMark(player, "@hx__fankui", table.concat(table.map(suits, function(s) return card_suits_table[s] end), ""))
    end
    local x = data.damage
    local target_role = (event == fk.Damaged) and data.from or data.to
    local choices = {"hx__fankui1", "hx__fankui2"}
    if player:getMark("hx__fankui_suitrecord") >= 4 then
      table.insert(choices, "beishui")
    end
    local choice = room:askToChoice(player,{
        choices = choices,
        skill_name = hx__fankui.name,
        prompt = "#hx__fankui-choose",
    })
    if choice == "beishui" then
      room:setPlayerMark(player, "hx__fankuiRecord", 0)
      room:setPlayerMark(player, "@hx__fankui", 0)
      room:setPlayerMark(player, "hx__fankui_suitrecord", 0)
    end
    if choice == "hx__fankui1" or choice == "beishui" then
      room:obtainCard(player, judge.card, false, fk.ReasonPrey)
      if target_role and target_role:isAlive() and not target_role:isNude() then
        local car = room:askToChooseCards(player, {
          target = target_role,
          min = x,
          max = x,
          flag = "he",
          skill_name = hx__fankui.name,
        })
        room:throwCard(car, self.name, target_role, player)
      end
    end
    if choice == "hx__fankui2" or choice == "beishui" then
      if target_role and target_role:isAlive() and not target_role:isNude() then
        local card = room:askToChooseCards(player, {
          target = target_role,
          min = 1,
          max = 1,
          flag = "he",
          skill_name = hx__fankui.name,
        })
        room:obtainCard(player, card, false, fk.ReasonPrey)
      end
      local to = room:askToChoosePlayers(player, {
        targets = room:getOtherPlayers(player, false),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__fankui-choose_target",
        skill_name = hx__fankui.name,
        cancelable = true,
      })
      if #to > 0 then
        to[1]:drawCards(x, self.name)
      end
    end
  end
})

return hx__fankui